<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="起始点" prop="startAddress">
      <el-input v-model="dataForm.startAddress" placeholder="起始点"></el-input>
    </el-form-item>
    <el-form-item label="结束点" prop="endAddress">
      <el-input v-model="dataForm.endAddress" placeholder="结束点"></el-input>
    </el-form-item>
    <el-form-item label="价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="价格" type="number" :step="0.1" min="0"></el-input>
    </el-form-item>
    <el-form-item label="客户" prop="customerId">
      <el-select filterable v-model="dataForm.customerId" placeholder="请选择">
        <el-option
          v-for="item in optionsCustomer"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="车辆" prop="carId">
      <el-select filterable v-model="dataForm.carId" placeholder="请选择">
        <el-option
          v-for="item in carOptions"
          :key="item.id"
          :label="item.carNum"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="支付方式" prop="carPayWay">
        <el-select v-model="dataForm.carPayWay">
          <el-option label="按里程支付" :value="1">按里程支付</el-option>
          <el-option label="一次性支付" :value="2">一次性支付</el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        optionsCustomer: [],
        carOptions: [],
        dataForm: {
          id: 0,
          startAddress: '',
          endAddress: '',
          customerId: '',
          carId: '',
          carPayWay: '',
          price: ''
        },
        dataRule: {
          startAddress: [
            { required: true, message: '起始点不能为空', trigger: 'blur' }
          ],
          endAddress: [
            { required: true, message: '结束点不能为空', trigger: 'blur' }
          ],
          customerId: [
            { required: true, message: '客户id不能为空', trigger: 'blur' }
          ],
          carId: [
            { required: true, message: '车辆id不能为空', trigger: 'blur' }
          ],
          carPayWay: [
            { required: true, message: '支付方式不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '价格不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.getcustomerList(),
      this.getCarList()
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/app/entcarcontract/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.startAddress = data.entCarContract.startAddress
                this.dataForm.endAddress = data.entCarContract.endAddress
                this.dataForm.customerId = data.entCarContract.customerId
                this.dataForm.carId = data.entCarContract.carId
                this.dataForm.carPayWay = data.entCarContract.carPayWay
                this.dataForm.price = data.entCarContract.price
                this.dataForm.status = data.entCarContract.status
              }
            })
          }
        })
      },
      getcustomerList(){
        // 客户接口
        this.$http({
          url: this.$http.adornUrl(`/app/entcustomer/listCustomer`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.optionsCustomer = data.list
          } else {
            this.optionsCustomer = []
          }
        })
      },
      getCarList(){
        // 客户接口
        this.$http({
          url: this.$http.adornUrl(`/app/entcar/infoList`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({ data }) => {
          console.log("xxxxx",data)
          if (data && data.code === 0) {
            this.carOptions = data.entCarList
          } else {
            this.carOptions = []
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/app/entcarcontract/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'startAddress': this.dataForm.startAddress,
                'endAddress': this.dataForm.endAddress,
                'customerId': this.dataForm.customerId,
                'carId': this.dataForm.carId,
                'carPayWay': this.dataForm.carPayWay,
                'price': this.dataForm.price,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
